#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc.h"
#include "../DEMURA/Demura.h"
void SD520_Enable_Flash_Control_and_SPI_Flash_En(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[5];
	u16 temp;
	u8 bufferx[] = {0x08,0xef,0x00,0x00,0x00,0x00,0x00,0x00,0xab};
	u8 RxBuff[8] = {0};
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);
	buffer1[0] = 0x03;
	buffer1[1] = 0x7f;
	buffer1[2] = 0xf0;
	buffer1[3] = 0x20;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	buffer1[0] = 0x03;
	buffer1[1] = 0xf0;
	buffer1[2] = 0xa5;
	buffer1[3] = 0x0f;
	buffer1[4] = 0xf0;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	SSD2828_W_Array(SigMode,channel,bufferx,0);
	bufferx[5] = 0x01;
	SSD2828_W_Array(SigMode,channel,bufferx,0);
	delay_ms(1000);
	bufferx[5] = 0x00;
	bufferx[8] = 0x9f;
	SSD2828_W_Array(SigMode,channel,bufferx,0);
	bufferx[5] = 0x01;
	SSD2828_W_Array(SigMode,channel,bufferx,0);
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Long_Read);
	SSD2828_W_Reg(SigMode,channel,0xC1,0x0008); //return package 
	SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); //reset
	buffer1[0] = 0x01;
	buffer1[1] = 0xf1;
	SSD2828_W_Array(SigMode,channel,buffer1,0); 
	delay_ms(15);
	SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
	SSD2828_W_Cmd(SigMode,channel,0xFF);
	for(i=0;i<8;i++)  //readback num
	{
		SSD2828_W_Cmd(SigMode,channel,0xFA);
		temp=SPI3_Read_u16_Data(channel);
		RxBuff[0+i] = temp>>8;
		RxBuff[1+i] = temp;
		delay_ms(5);
		i++;
	}
	buffer[4] = RxBuff[5];
	buffer[5] = RxBuff[6];
	buffer[6] = RxBuff[7];

	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);  
	
}
void SD520_Inter_Demura_Ram(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[5];
	u8 bufferx[18] = {0x11,0xb0,0xf0,0x00,0x00,0x78,0x96,0x72,0x20,0x30,0x10,0x06,0x01,0xff,0xff,0x38,0xf7,0x00};
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);		
	delay_ms(5);
	buffer1[0] = 0x03;
	buffer1[1] = 0x7f;
	buffer1[2] = 0xf0;
	buffer1[3] = 0x20;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(5);
	buffer1[0] = 0x04;
	buffer1[1] = 0xf0;
	buffer1[2] = 0xa5;
	buffer1[3] = 0x0f;
	buffer1[4] = 0xf0;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(5);
	buffer1[0] = 0x03;
	buffer1[1] = 0xe5;
	buffer1[2] = 0xf0;
	buffer1[3] = 0x00;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(5);
	SSD2828_W_Array(SigMode,channel,bufferx,0);
	delay_ms(5);
	buffer1[0] = 0x03;
	buffer1[1] = 0xe0;
	buffer1[2] = 0xf0;
	buffer1[3] = 0x01;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(15);     												
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);	
}
                                    
void SD520_Exit_Demura_Ram(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}
                             
void SD520_Flash_Write_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		u8 buffer1[5];
		u16 temp = 0;
		u8 bufferx[] = {0x1f,0xEF,0x00,0x00,0x01,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x10,0x00,0x38,0x72,0x1f,0x38,0x72,0x1f,0x28,0x03,0x1f,0x03,0x33,0x03,0x00,0x00,0x00,0x00,0x00,0x00};
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);	
		buffer1[0] = 0x03;
		buffer1[1] = 0x7f;
		buffer1[2] = 0xf0;
		buffer1[3] = 0x20;
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		buffer1[0] = 0x04;
		buffer1[1] = 0xf0;
		buffer1[2] = 0xA5;
		buffer1[3] = 0x0F;
		buffer1[4] = 0xf0;
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		buffer1[0] = 0x03;
		buffer1[1] = 0xbd;
		buffer1[2] = 0xf0;
		buffer1[3] = 0x64;
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		//delay_ms(5);   
		SSD2828_W_Array(SigMode,channel,bufferx,0);
		//delay_ms(5); 
		buffer1[0] = 0x02;
		buffer1[1] = 0xef;
		buffer1[2] = 0x01;
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		
			SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Long_Read);
			SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package 
			SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); //reset
		do{
				buffer1[0] = 0x01;
				buffer1[1] = 0xf1;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 
				
				delay_ms(15);
				SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
				SSD2828_W_Cmd(SigMode,channel,0xFF); 
				
				SSD2828_W_Cmd(SigMode,channel,0xFA);
				buffer[4]=SPI3_Read_u16_Data(channel)>>8;
				delay_ms(5);
				temp++;
		}while(((buffer[4]&0x01) != 0x00) && temp < 2000);
		if(temp>=2000)
		{
			buffer[4] = 0xff;
		}		
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);	
}                       

void SD520_Flash_Write_Demura2(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}


void SD520_Flash_Check_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	  u8 bufferx[2];
		u16 temp;
		u8 buffer1[] = {0x03,0x7f,0xf0,0x20};
		u8 buffer2[] = {0x04,0xf0,0xa5,0x0f,0xf0};
		u8 buffer3[] = {0x08,0xef,0x00,0x00,0x00,0x00,0x00,0x07,0xab};
		u8 buffer4[] = {0x05,0xef,0x00,0x00,0x00,0x01};
		u8 buffer5[] = {0x0d,0xef,0x00,0x00,0x00,0x00,0x00,0x07,0x03,0x00,0x00,0x00,0x83,0x1f};
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);
		delay_ms(15);
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		SSD2828_W_Array(SigMode,channel,buffer2,0);
		SSD2828_W_Array(SigMode,channel,buffer3,0);
		SSD2828_W_Array(SigMode,channel,buffer4,0);
		delay_ms(15);
		for(int i=0;i<4;i++)
		{
			if(i==0)
			{
				buffer5[11] = 0x00;
			}
			else if(i==1)
			{
				buffer5[11] = 0x01;
			}
			else if(i==2)
			{
				buffer5[11] = 0x02;
			}
			else if(i==3)
			{
				buffer5[11] = 0x03;
			}
				SSD2828_W_Array(SigMode,channel,buffer5,0);
				SSD2828_W_Array(SigMode,channel,buffer4,0);
			
			SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Long_Read);
			SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package 
			SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); //reset
			
				bufferx[0] = 0x01;
				bufferx[1] = 0xf1;
				SSD2828_W_Array(SigMode,channel,bufferx,0); 
				
				delay_ms(15);
				SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
				SSD2828_W_Cmd(SigMode,channel,0xFF); 
				
				SSD2828_W_Cmd(SigMode,channel,0xFA);
				temp=SPI3_Read_u16_Data(channel);
				buffer[4+i*2] = temp;
				buffer[4+i*2+1] = temp>>8;
				delay_ms(5);
			
				SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);
				
				STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);	
		}
		

}
                                  
void SD520_Flash_Erase_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{	
	u8 buffer1[] = {0x03,0x7f,0xf0,0x20};
	u8 buffer2[] = {0x04,0xf0,0xa5,0x0f,0xf0};
	u8 buffer3[] = {0x07,0xef,0x00,0x00,0x01,0x00,0x00,0x07};
	u8 buffer4[] = {0x08,0xef,0x00,0x00,0x00,0x00,0x00,0x00,0xAB};
	u8 buffer5[] = {0x05,0xef,0x00,0x00,0x00,0x01};
	u8 buffer6[] = {0x08,0xef,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
	u8 buffer7[] = {0x08,0xef,0x00,0x00,0x00,0x00,0x00,0x00,0x06};
	u8 buffer8[] = {0x08,0xef,0x00,0x00,0x00,0x00,0x00,0x00,0xc7};
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);
	delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer2,0);
		delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer3,0);
		delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer4,0);
		delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer5,0);
		delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer3,0);
		
	delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer7,0);
	delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer5,0);

	delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer6,0);
	delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer5,0);
	
	delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer7,0);
	delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer5,0);
	delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer8,0);
	delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer5,0);
	delay_ms(3000);
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //陏?Y3??crc????

}

void SD520_Flash_Demura_OTP(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}

void SD520_Demura_Function_ON(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	
	
//	u8 buffer1[] = {0x03,0x7f,0xf0,0x20};
//	u8 buffer2[] = {0x04,0xf0,0xa5,0x0f,0xf0};
//	u8 buffer3[] = {0x11,0xb0,0Xf0,0x00,0x00,0x78,0x96,0x72,0x20,0x30,0x10,0x06,0x01,0xff,0xff,0x38,0xf7,0x00};
//	u8 buffer4[] = {0x04,0xe5,0xf0,0x01};
//	u8 buffer5[] = {0x62,0xe6,0xf0,0x80,0x00,0x00,0x12,0x84,0x09,0xc0,0x20,0x15,0x00,0x68,0x04,0x4e,0x03,0x68,
//									0x04,0x77,0x01,0x00,0x02,0xac,0x03,0x20,0x00,0x0d,0x00,0x07,0x00,0xbc,0x04,0x34,0x05,0x20,
//									0x09,0x00,0x18,0x00,0x20,0x0c,0x03,0x33,0x0b,0x0b,0x06,0x38,0x2a,0x1c,0x0e,0x69,0x62,0x54,
//									0x46,0x7b,0x79,0x77,0x70,0x02,0x01,0x7e,0x7d,0x40,0x09,0x00,0x01,0xfc,0x19,0xbe,0x09,0xf8,
//									0x19,0xfa,0x19,0x78,0x1a,0x38,0x1a,0xb6,0x2a,0xb6,0x1a,0xf4,0x2a,0xf4,0x2a,0x74,0x63,0x34,
//									0x4b,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
//	u8 buffer6[] = {0x1f,0xef,0x00,0x00,0x01,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x10,0x00,0x38,0x72,0x1f,0x38,
//									0x72,0x1f,0x28,0x03,0x1f,0x03,0x33,0x03,0x00,0x00,0x00,0x00,0x00,0x00};
//	u8 buffer7[] ={0x03,0xe8,0xf0,0x03};
//	u8 buffer8[] = {0x03,0xc8,0xf0,0x11};
//	u8 buffer9[] = {0x07,0xe0,0xf0,0x41,0x6f,0x03,0x61,0x00};
//	u8 buffer10[2] ;
//	SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);		
//	buffer10[0] = 0x01;
//	buffer10[1] = 0x11;
//	SSD2828_W_Array(SigMode,channel,buffer10,0);	
// 	SSD2828_W_Array(SigMode,channel,buffer1,0);
//	SSD2828_W_Array(SigMode,channel,buffer2,0);
//	SSD2828_W_Array(SigMode,channel,buffer3,0);
//	SSD2828_W_Array(SigMode,channel,buffer4,0);
//	SSD2828_W_Array(SigMode,channel,buffer5,0);
//	SSD2828_W_Array(SigMode,channel,buffer6,0);			
//	SSD2828_W_Array(SigMode,channel,buffer7,0);		
//	SSD2828_W_Array(SigMode,channel,buffer8,0);		
//	SSD2828_W_Array(SigMode,channel,buffer9,0);	
//	buffer10[0] = 0x01;
//	buffer10[1] = 0x29;
//	SSD2828_W_Array(SigMode,channel,buffer10,0);			
	
	u8 buffer1[5];
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);	
	buffer1[0] = 0x03;
	buffer1[1] = 0xc8;
	buffer1[2] = 0xf0;
	buffer1[3] = 0x11;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	buffer1[0] = 0x03;
	buffer1[1] = 0xe0;
	buffer1[2] = 0xf0;
	buffer1[3] = 0x01;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	
}

void SD520_Demura_Function_OFF(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[5];
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);	
	buffer1[0] = 0x03;
	buffer1[1] = 0xe0;
	buffer1[2] = 0xf0;
	buffer1[3] = 0x40;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	buffer1[0] = 0x03;
	buffer1[1] = 0xc8;
	buffer1[2] = 0xf0;
	buffer1[3] = 0x00;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}
void SD5202_GM583_Write_Demura_Ram(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
		u8 buf2PC[10];
	DemuraTransOnce=120;	
	if(USB_Rx_Demura==0)//第一笔数据
	{	
			buffer[1] = 0x2c;
			USB_Rx_Demura=1; //第一次进入Demura数据写入状态
	}
	else buffer[1] = 0x3c;
	buffer[0] = 0x79;         
	for(i=2;i<USB_ReceivedCount+2;i++)
	{
		buffer[i+VCP_Receive_True_num] = USB_Rx_Buffer[i-2];  //
	}
	VCP_Receive_True_num+=USB_ReceivedCount;  //统计是否攒够一行
	DemuraRecivedNum+=USB_ReceivedCount;	  //统计总的接收数量	
	if((VCP_Receive_True_num==DemuraTransOnce)||(DemuraRecivedNum==USB_Rx_Demura_Total_Num)) //写入单笔数据,单次写入OLED.H_pixel*3字节
	{																					//最后一笔会多送数据，但是多送的数据不会被IC接收	

		SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);			
		SSD2828_W_Array(SigMode,channel,buffer,0);
		VCP_Receive_True_num=0;
		
		if(DemuraRecivedNum==USB_Rx_Demura_Total_Num)//最后一笔，状态重置
		{
			DemuraRecivedNum=0; //清空 累计接受数据
			USB_Rx_Demura=0;	  //清空 Demura downloading状态
			Demura_START_FLAG=0;//清空 开始Demura传输的标志位
			STM2PC(pdev,CDC_IN_EP,buf2PC,5+3);           //返回上位机5个byte数据
		}
	}
}
void ProcessForDmuR10( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	switch(USB_Rx_Buffer[1])
	{
			case 0x01:                                      //????
					SD520_Inter_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x02:                                      //????
					SD520_Exit_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;						 
			case 0x03:                                      //????
					SD520_Flash_Write_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x04:                                      //????
					SD520_Flash_Check_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;												
			case 0x05:                                      //????
					SD520_Flash_Erase_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			case 0x06:                                      //check flash ID
					SD520_Enable_Flash_Control_and_SPI_Flash_En(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x07:                                      //check flash ID
					SD520_Demura_Function_ON(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x08:                                      //check flash ID
					SD520_Demura_Function_OFF(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;													
			case 0x0A:
					SD520_Flash_Write_Demura2(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			default:
				break;												
	}
}
//********************************************************************************************